﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhy.HelpClassLibrary.Models;
using Zhy.HelpClassLibrary.Utility.TxtHelper;

namespace Zhy.HelpClassLibrary.Demo
{
    /// <summary>
    /// txt文本文件操作帮助类
    /// 这个是txt文本文件操作类，这里的代码能看懂就可以
    /// </summary>
    public class TxtDemoShow
    {
        //存储路径
        public static string storePath = "";
        //配置文件的路径
        public static string setFilePath = "";
        //名目文件路径
        public static string itemPath = "";
        //收入信息文件路径 
        public static string incomePath = "";
        //支出信息文件路径
        public static string expendPath = "";
        //日统计文件路径
        public static string dayPath = "";
        //周统计文件路径
        public static string weekPath = "";
        //月统计文件路径
        public static string monthPath = "";

        /// <summary>
        /// 读取txt文本的所有行数据
        /// 查询指定类别名目
        /// </summary>
        /// <returns></returns>
        public static List<string> GetItemList()
        {
            List<string> items = new List<string>(); // txt文本所有行集合
            if (File.Exists(itemPath))
            {
                List<string> lines = TextHelper.ReadTextAllLines(itemPath); // 读取txt文件所有文本行
                //第一行是标题，从第二行开始读
                if (lines.Count > 1) // 判断文本要大于一行
                {
                    items = lines.Skip(1).ToList(); // 绕过序列中指定数量的元素，然后返回剩余的元素。
                }
            }
            return items; // 返回数据
        }

        /// <summary>
        /// 根据名目类别获取名目名称列表  itemType=1 收入   2 支出
        /// </summary>
        /// <param name="itemType"></param>
        /// <returns></returns>
        public static List<string> GetItems(int itemType)
        {
            List<string> itemNames = new List<string>();
            List<string> items = GetItemList(); // 获取txt文本所有行数据
            if (items.Count > 0)
            {
                string typeName = itemType == 1 ? "收入" : "支出";
                items.ForEach(item => // 循环所有行文本
                {
                    string[] arr = item.Split(';'); // 获取每行数据信息
                    if (arr[1] == typeName) // 判断收入
                        itemNames.Add(arr[2]); // 获取收入或支出的值
                });
            }
            return itemNames; // 返回收入或支出的值
        }

        /// <summary>
        /// 获取所有的收入或支出记录列表   1、2
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<RecordInfo> GetRecordList(int type)
        {
            List<RecordInfo> recordList = new List<RecordInfo>();
            string path = "";
            if (type == 1)
                path = incomePath;
            else
                path = expendPath;
            if (File.Exists(path))
            {
                List<string> lines = TextHelper.ReadTextAllLines(path);
                if (lines.Count > 1)
                {
                    lines = lines.Skip(1).ToList();
                    foreach (string line in lines)
                    {
                        string[] arr = line.Split(';');
                        RecordInfo record = new RecordInfo()
                        {
                            Id = int.Parse(arr[0]),
                            ItemName = arr[1],
                            Remark = arr[2],
                            Money = decimal.Parse(arr[3]),
                            NoteTime = arr[4]
                        };
                        recordList.Add(record);
                    }
                }
            }

            return recordList;
        }

        //生成记录字符串
        public static string GetRecordString(RecordInfo record)
        {
            string str = $"{record.Id};{record.ItemName};{record.Remark};{record.Money.ToString("0.00")};{record.NoteTime}";
            return str;
        }

        //加载所有记录（收入、支出）
        public static List<RecordInfo> GetAllRecords()
        {
            List<RecordInfo> records = new List<RecordInfo>();
            List<RecordInfo> incomeList = GetRecordList(1);
            List<RecordInfo> expendList = GetRecordList(2);
            int id = 0;
            if (incomeList.Count > 0)
            {
                foreach (RecordInfo record in incomeList)
                {
                    id += 1;
                    record.Id = id;
                    record.ItemType = "收入";
                    records.Add(record);
                }
            }
            if (expendList.Count > 0)
            {
                foreach (RecordInfo record in expendList)
                {
                    id += 1;
                    record.Id = id;
                    record.ItemType = "支出";
                    records.Add(record);
                }
            }
            return records;
        }
    }
}
